//
//  PlayListTableView.m
//  GC_OC
//
//  Created by wlf on 2022/5/21.
//

#import "PlayListTableView.h"
#import "PlayListTableViewCell.h"
#import "Common.h"
@implementation PlayListTableView

- (instancetype)initWithFrame:(CGRect)frame history:(BOOL)isHistory {
    if (self = [super initWithFrame:frame]) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 17, 458) style:UITableViewStylePlain];
        self.tableView.showsVerticalScrollIndicator = NO;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.isHistory = isHistory;
        self.tableView.delegate  = self;
        self.tableView.dataSource = self;
        UIView *footerView = [[UIView alloc]  initWithFrame:CGRectMake(0, 0,  SCREEN_WIDTH - 17, HY_BottomWhiteBar_Height + 10)];
        footerView.backgroundColor = [UIColor whiteColor];
        self.tableView.tableFooterView = footerView;
        [self addSubview:self.tableView];
    }
    return self;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
}

- (void)setCurrentArray:(NSArray *)currentArray
{
    _currentArray = currentArray;
    [self.tableView reloadData];
    self.backgroundColor = [UIColor whiteColor];
}
- (void)setSelectIndex:(NSInteger)selectIndex{
    _selectIndex = selectIndex;
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:selectIndex inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    
}
- (void)setSubTitleStr:(NSString *)subTitleStr{
    _subTitleStr = subTitleStr;
    
}
- (void)layoutSubviews{
    [super layoutSubviews];

}
- (void)clearAllData{
    _currentArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"getcatedetail"];
    _currentArray = @[];
    [[NSUserDefaults standardUserDefaults] setValue:_currentArray forKey:@"getcatedetail"];
    [self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.currentArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!self.isHistory) {
        NSDictionary *dict = self.currentArray[indexPath.row];

        PlayListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"_currentTabView"];
        if (cell == nil) {
            cell = [[PlayListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"_currentTabView"];
        }
        cell.titleLab.text = dict[@"name"];
        [Common updataTimerLableWithLable:cell.subTitleLab
                                   Second:[dict[@"audio_num"] integerValue]];
        cell.clearBtn.hidden = YES;
        return cell;
    }else{
        NSDictionary *dict = [toolHelp dictionaryWithJsonString:self.currentArray[indexPath.row][@"data"]];
        PlayListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"_historyTabView"];
        if (cell == nil) {
            cell = [[PlayListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"_historyTabView"];
        }
        cell.titleLab.text = dict[@"name"];
        cell.subTitleLab.text = [NSString setSafeString:dict[@"media_class_name"]];
//        cell.subTitleLab.font = cell.titleLab.font;
        cell.clearBtnClick = ^{
            self.currentArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"getcatedetail"];
            NSMutableArray *mutArray = [NSMutableArray arrayWithArray:self.currentArray];
            [mutArray removeObjectAtIndex:indexPath.row];
            self.currentArray = mutArray.copy;
            [[NSUserDefaults standardUserDefaults] setValue:self.currentArray forKey:@"getcatedetail"];
            [self.tableView reloadData];
        };
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    NSArray *dataArray = [ICMusicPlayModel arrayOfModelsFromDictionaries:self.currentArray error:nil];
    if (self.listPlayIndexBlock) {
        self.listPlayIndexBlock(self.currentArray, indexPath.row);
    }
  
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
//- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
//    UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_WIDTH-17, 458) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
//    CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
//    maskLayer1.frame = CGRectMake(0, 0, SCREEN_WIDTH-17, 458);
//    maskLayer1.path = maskPath1.CGPath;
//    view.layer.mask = maskLayer1;
//}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 17, 50)];
    view.backgroundColor = [UIColor whiteColor];
   
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 10, SCREEN_WIDTH - 30, 30)];
    label.font = [UIFont boldSystemFontOfSize:16];
    label.textColor = [UIColor blackColor];
    if (!_isHistory) {
        label.text = [NSString stringWithFormat:@"当前播放"];
    }else{

        NSString  *str = [NSString stringWithFormat:@"上次记录播放%@(%ld)", self.subTitleStr ?: @"", self.currentArray.count];
        NSMutableAttributedString *mutStr = [[NSMutableAttributedString alloc] initWithString:str];
        [mutStr addAttributes:@{
            NSFontAttributeName: [UIFont systemFontOfSize:12],
            NSForegroundColorAttributeName: [UIColor colorWithHex:@"#6D7278"]
        } range:[str rangeOfString:[NSString stringWithFormat:@"%@(%ld)", self.subTitleStr, self.currentArray.count]]];
        label.attributedText = mutStr;
    }

    [view addSubview:label];
    if (_isHistory) {
        UIButton *clearBtn = [[UIButton alloc] init];
        [clearBtn  setBackgroundImage:[UIImage imageNamed:@"icon_删除_32_default"] forState:UIControlStateNormal];
        clearBtn.size = CGSizeMake(32, 32);
        clearBtn.centerY = label.centerY;
        clearBtn.left = SCREEN_WIDTH - 42 - 17;
        [clearBtn addTarget:self action:@selector(clearAllData) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:clearBtn];
    }
    return view;
}
@end
